package jian.hamster.ui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	
	import jian.hamster.controller.TimerController;
	import jian.hamster.event.GameEvent;
	
	/**
	 * 倒计时文本
	 * @author jianlong.chen
	 * 
	 */	
	public class CountDownTxt extends Sprite
	{
		private var _txt:TextField;
		private var _startCount:int;
		private var _fullCount:int;
		
		public function CountDownTxt($startCount:int)
		{
			_txt = new TextField();
			_fullCount = _startCount = $startCount;
			_txt.text=''+_startCount+' 秒';
			addChild(_txt);
			TimerController.instance.addTickerCallBack(1000,tickerHandler);
		}
		
		protected function __onTickerSecond(event:Event):void
		{
			if(_startCount <= 0)
			{
				TimerController.instance.removeEventListener(GameEvent.ENTER_FRAME,__onTickerSecond);
			}
			else
			{
				_txt.text=''+(--_startCount) + ' 秒';
			}			
		}
		
		private function tickerHandler():void
		{
			if(_startCount <= 0)
			{
				_startCount=_fullCount;
				_txt.text=''+(_startCount) + ' 秒';
//				TimerController.instance.deleteTickerCallBack(tickerHandler);
			}
			else
			{
				_txt.text=''+(--_startCount) + ' 秒';
			}
		}
	}
}